package com.wang.users.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.mapper.AroleMapper;
import com.wang.users.pojo.Arole;
import com.wang.users.pojo.AroleQuery;
import com.wang.users.service.AroleService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 角色Service的实现类
 * @author Zjx
 */
@Service
@Log4j2
public class AroleServiceImpl extends ServiceImpl<AroleMapper,Arole> implements AroleService {
    /**
     * 对父类超级不满意
     * 顶了一个编程式事务
     * @param entity 实体对象
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Arole entity) {
        /* 一些用户不需要操作的字段,需要系统自动赋值的放到此处 */
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());

        boolean dbFlag = super.save(entity);
//        log.info("==保存成功:{}==", dbFlag);
//        String str = null;
//        str.toString();
        return dbFlag;
    }

    @Override
    public boolean updateById(Arole entity) {
        entity.setUpdateTime(new Date());
        return super.updateById(entity);
    }

    @Override
    public List<Arole> findList(PageInfoUtil pageInfoUtil, AroleQuery param) {
        /* 正研的分页对象和mybatisPlus的分页对象要转换 */
        IPage<Arole> page = new Page<>(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
        /* 为keyword增加% */
        if (StringUtils.isNotEmpty(param.getKeyword())) {
            param.setKeyword("%" + param.getKeyword() + "%");
        }
        List<Arole> list = this.baseMapper.findList(page, param);
        /* 设置总条数 */
        pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
        return list;
    }

    @Override
    public List<Arole> findList(AroleQuery param) {
        return this.baseMapper.findList(param);
    }
}
